/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.repository;

import com.bifrost.entity.TransactionSendLog;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Collection;
import java.util.List;

/**
 *
 *
 *@author joewee
 *@version 1.0.0
 *@Date 2018/5/18 17:26
 */
public interface TransactionSendLogRepository extends JpaRepository<TransactionSendLog,Long> {
    /**
     * 安装订单id和类型查询
     * @param id
     * @param type
     * @return
     */
    TransactionSendLog findFirstByOrderIdAndType(Long id, TransactionSendLog.Type type);

    List<TransactionSendLog> findBySuccess(Boolean isSuccess);
    List<TransactionSendLog> findBySuccess(Boolean isSuccess, Pageable pageable);

    List<TransactionSendLog> findByRichBaseReceipt(Boolean isSuccess, Pageable pageable);

    List<TransactionSendLog> findByReportSendSuccess(Boolean isSuccess);
    List<TransactionSendLog> findByReportSendSuccess(Boolean isSuccess, Pageable pageable);

    @Modifying
    @Query("update TransactionSendLog t set t.success = ?1,t.sendDate =?2 where t.id in ?3")
    @Transactional
    int updateReceiptRalatedFiled(Boolean success,Date sendDate,List<Long> ids);
    /**
     * 更新交易小票发送状态为成功
     * @param ids
     * @return
     */
    @Query(value = "update TRANSACTION_SEND_LOG SET IS_SUCCESS = 1,SEND_DATE = sysdate(),REMARK = null WHERE ID IN (?1)",nativeQuery = true)
    @Modifying
    @Transactional
    int setTransactionSuccess(Collection<Long> ids);

    /**
     * 更新富基交易小票发送状态为成功
     * @param ids
     * @return
     */
    @Query(value = "update TRANSACTION_SEND_LOG SET RICHBASE_RECEIPT_SEND = 1,RICHBASE_RECEIPT_SEND_DATE = sysdate() WHERE ID IN (?1)",nativeQuery = true)
    @Modifying
    @Transactional
    int setTransactionRichBaseSuccess(Collection<Long> ids);

    @Modifying
    @Query("update TransactionSendLog t set t.reportSendSuccess =?1,t.reportSendDate = ?2 where t.id in ?3")
    @Transactional
    int updateReportRalatedFild(Boolean reportSendSuccess,Date reportSendDate,List<Long> ids);

    /**
     * 按状态查询某日期前的交易数据
     * @param date
     * @param isSuccess
     * @return
     */
    List<TransactionSendLog> findByCreateDateBeforeAndReportSendSuccess(Date date,Boolean isSuccess);

    /**
     * 按状态查询某日期前小票发送成功的交易
     * @param date
     * @param isSuccess
     * @return
     */
    List<TransactionSendLog> findByCreateDateBeforeAndReportSendSuccessAndSuccessIsTrue(Date date,Boolean isSuccess);

    /**
     * 获取所有type订单
     * @return
     */
    @Query(value = " SELECT * FROM TRANSACTION_SEND_LOG T WHERE T.TYPE<>'CANCEL' AND T.SEND_RECORD_STATUS='N' OR T.SEND_RECORD_STATUS IS NULL",nativeQuery = true)
    List<TransactionSendLog> findByTypeAndSendRecordStatus();

    /**
     * 分页所有type订单
     * @return
     */
    //@Query(value = " SELECT * FROM TRANSACTION_SEND_LOG T WHERE T.TYPE<>'CANCEL' AND T.SEND_RECORD_STATUS='N' OR T.SEND_RECORD_STATUS IS NULL",nativeQuery = true)
    List<TransactionSendLog> findByTypeNotAndSendRecordStatus(TransactionSendLog.Type type , String status , Pageable request);

    @Modifying
    @Transactional
    @Query(value = "UPDATE TRANSACTION_SEND_LOG SET SEND_RECORD_STATUS=?2 WHERE ID IN (?1)",nativeQuery = true)
    int batchUpdateSysStatus(Collection ids , String status);

    @Modifying
    @Transactional
    @Query(value = "UPDATE TRANSACTION_SEND_LOG SET SEND_RECORD_STATUS=?3 , SEND_RECORD_REMARK=?2 WHERE ID = ?1",nativeQuery = true)
    int singleUpdateSysStatus(Long id , String remark , String status);

    @Query(value = " SELECT * from transaction_send_log where IS_SUCCESS=0 and CREATE_DATE<'2018-10-26'",nativeQuery = true)
    List<TransactionSendLog> findByCurrSendReceipt();
}
